// pages/blog/blog.js
const db = wx.cloud.database()
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    widheight:0,
    likeCount: 0,
    likeStatus: false,
    activeIndex: 0,
    list: [],
    year: 0,
    mouth: '',
    day: 0,
    change: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          widheight: res.screenHeight-120,
        });
      }
    });
    this._getList()
    //this._getUserInfo()
  },

  

  //获取期刊
  _getList() {
    let w = {
      isShow: true
    }
    db.collection('qikan').where(w).orderBy('createTime', 'desc').limit(10).get().then((res)=> {
      this.setData({
        list: res.data,
        likeCount: res.data[0].likeNums
      })
      this._formatTime(res.data[0].createTime)
      db.collection('qikan-like').where({_openid: app.globalData.openid, q_id: res.data[0].id}).get().then((res)=> {
        if (res) {
          this.setData({
            likeStatus: res.data[0].islike
          })
        }
      })
    })
  },

  _formatTime(date) {
    let months = [
      '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'
    ]
    this.setData({
      year: date.getFullYear(),
      month: months[date.getMonth()],
      day: date.getDate()
    })
   },
   changeLike(event) {
     this.setData({
        likeCount: this.data.likeCount + event.detail.count,
        likeStatus: event.detail.isLike
     })
   },
   changeQk(event) {
      let detail = this.data.list[event.detail.current]
      this.setData({
        change: true
      })
      //console.log(detail)
      this._formatTime(detail.createTime)
      db.collection('qikan-like').where({_openid: app.globalData.openid, q_id: detail.id}).get().then((res)=> {
        //console.log(res)
        if (res.data) {
          this.setData({
            likeStatus: res.data[0].islike,
            likeCount: detail.likeNums
          })
        }
      })
   },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})